package com.example.news.dao;

import com.example.news.entity.User;

import java.util.List;
import java.util.Objects;

/**
 * @author Admin
 */
public class UserDao extends User {

    public static long queryPhone(String phone) {
        return count(User.class, "phone =?", new String[]{phone});
    }

    public static long queryLogin(String userName, String userPwd) {
        return User.count(User.class, "phone=? and pwd=?", new String[]{userName, userPwd});
    }

    public static User queryLoginUser(String userName, String userPwd) {
        List<User> list = User.find(User.class, "phone=? and pwd=?", new String[]{userName, userPwd});
        if (Objects.nonNull(list) && list.size() == 1) {
            return list.get(0);
        }
        return null;
    }

    public static User queryLoginUser(String userName) {
        List<User> list = User.find(User.class, "phone=?", new String[]{userName});
        if (Objects.nonNull(list) && list.size() == 1) {
            return list.get(0);
        }
        return null;
    }

    public static boolean changeUpdate(String name, String newPwd) {
        User user = queryLoginUser(name);
        if (Objects.nonNull(user)) {
            user.setPwd(newPwd);
            User.update(user);
            return true;
        }

        return false;
    }


}
